import router from "@src/router";

Component({
  options: {
    addGlobalClass: true
  },
  properties: {
    imageList: {
      type: Array,
      value: []
    },
    shopInfo: {
      type: Object,
      value: {}
    },
    supplyFacility: {
      type: String,
      value: null
    },
    supplyService: {
      type: String,
      value: null
    },
    themeColor: {
      type: String,
      value: null
    },
  },

  data: {
    supplyList: [],
    indicatorIndex: 1,
  },
  observers: {
    'supplyFacility': function () {
      let supplyFacility = this.parseStr(this.data.supplyFacility);
      let supplyList = []
      let otherList = []
      if (supplyFacility) {
        supplyFacility.forEach(item => {
          if (item.label === '其它服务') {
            otherList.push(...item.checkList)
          } else {
            supplyList.push(...item.checkList)
          }
        })
      }
      let supplyService = this.parseStr(this.data.supplyService);
      if (supplyService) {
        supplyService.forEach(item => {
          if (item.label === '其它服务') {
            otherList.push(...item.checkList)
          } else {
            supplyList.push(...item.checkList)
          }
        })
      }
      supplyList.push(...otherList)
      if (supplyList.length > 3) {
        supplyList = supplyList.slice(0, 3)
      }
      this.setData({supplyList: supplyList})
    },
  },
  methods: {
    parseStr(jsonStr) {
      if (!jsonStr) {
        return null;
      }
      try {
        return JSON.parse(jsonStr)
      } catch (e) {
        console.info("解析异常", e, jsonStr)
      }
      return null;
    },
    handleToShopDetail(e) {
      router.push({
        name: 'booking/shopDetail',
        params: {shopId: this.data.shopInfo.shopId, navIndex: e.currentTarget.dataset.index}
      })
    },
    handleToShopList() {
      // this.triggerEvent('shoplist')
	  router.push({
		name: 'mall/store-list',
		params: { shopId: this.data.shopInfo.shopId,storeType:'booking' }
	  })
    },
    handleToMap() {
      const latitude = this.data.shopInfo.latitude
      const longitude = this.data.shopInfo.longitude
      const name = this.data.shopInfo.name
      const address = this.data.shopInfo.address
      wx.openLocation({latitude, longitude, name, address, scale: 18})
    },
    handleImageChange(e) {
      let indicatorIndex = e.detail.current + 1;
      this.setData({indicatorIndex: indicatorIndex})
    }
  }
});
